package com.ssms.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class R<T> {

    private Integer code;

    private String msg; //错误信息

    private T data; //数据

    //private Map map = new HashMap(); //动态数据

    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.msg = "success";
        r.data = object;
        r.code = 200;
        return r;
    }
    //专用于登录返回token和角色属性的构造方法
    public static <T> R<T> success(T object,String role,String token) {
        R<T> r = new R<T>();
        r.msg = "success";
        r.data = (T) new MyData(token,role);
        r.code = 200;
        return r;
    }
    //操作成功后的数据封装
    public static <T> R<T> success(T data,String msg) {
        R<T> r = new R<T>();
        r.code = 200;
        r.msg = msg;
        r.data = data;
        return r;
    }
    public static <T> R<T> success(String msg) {
        R<T> r = new R<T>();
        r.msg = msg;
        r.code = 200;
        return r;
    }

    public static <T> R<T> error(String msg) {
        R r = new R();
        r.msg = msg;
        r.code = 200;
        return r;
    }

    public static <T> R<T> error(Object n,String msg) {
        R r = new R();
        r.data = new MyData(n);
        r.msg = msg;
        r.code = 200;
        return r;
    }
    public static <T> R<T> error(ResponseDataEnum responseDataEnum){
        R<T> r = new R<T>();
        r.code = responseDataEnum.getCode();
        r.data = null;
        r.msg = responseDataEnum.getMsg();
        return  r;
    }

    public static <T> R<T> error(Integer code,String msg){
        R<T> r = new R<T>();
        r.code = code;
        r.data = null;
        r.msg = msg;
        return  r;
    }
}
